--include the Stargate Base tool
include("weapons/gmod_tool/stargate_base_tool.lua");
--Our Current Category ... im going to move this to the config file soon.
TOOL.Category="Janus's Lantis Tech";
TOOL.Name="#Touchstone";

-- The default model for the GhostPreview
TOOL.ClientConVar["model"] = "models/props_junk/PopCan01a.mdl";
TOOL.ClientConVar["autoweld"] = 1;

-- Information about the SENT to spawn
TOOL.Entity.Class = "touchstone";
TOOL.Entity.Limit = 1;

-- Add the topic texts
TOOL.Topic["name"] = "Stargate Touchstone";
TOOL.Topic["desc"] = "Creates the Touchstone";
TOOL.Topic[0] = "Left click, to spawn the touchstone";

--Language
TOOL.Language["Undone"] = "Touchstone removed";
TOOL.Language["Cleanup"] = "Touchstone's";
TOOL.Language["Cleaned"] = "Removed all Touchstone's";
TOOL.Language["SBoxLimit"] = "Too many Touchstones";


--################# LeftClick Toolaction @aVoN
function TOOL:LeftClick(t)
	if(t.Entity and t.Entity:IsPlayer()) then return false end;
	if(CLIENT) then return true end;
	local p = self:GetOwner();
	local toggle = self:GetClientNumber("toggle");
	local model = self:GetClientInfo("model");
	local size = 250;
	local immunity = self:GetClientNumber("immunity");
	local strength = self:GetClientNumber("strength");
	local bubble = self:GetClientNumber("bubble");
	-- Due to compatibility issues with Gmod2007, we need to divide by 255
	local r = self:GetClientNumber("r")/255;
	local g = self:GetClientNumber("g")/255;
	local b = self:GetClientNumber("b")/255;
	local containment = self:GetClientNumber("containment");
	local passing_draw = self:GetClientNumber("passing_draw");
	--######## Spawn SENT
	if(t.Entity and t.Entity:GetClass() == self.Entity.Class) then
		t.Entity:SetSize(size);
		t.Entity.ImmuneOwner = false;
		if(util.tobool(immunity)) then
			t.Entity.ImmuneOwner = true;
		end
		t.Entity.DrawBubble = false;
		if(util.tobool(bubble)) then
			t.Entity.DrawBubble = true;
		end
		t.Entity:SetMultiplier(strength);
		t.Entity:SetShieldColor(r,g,b);
		t.Entity.PassingDraw = util.tobool(passing_draw);
		t.Entity.Containment = util.tobool(containment);
		-- Make changes take effect immediately, when shield is turned on
		if(t.Entity:Enabled()) then
			t.Entity:Status(false,true);
			local e = t.Entity;
			timer.Simple(0.1,
				function()
					if(e and e:IsValid()) then
						e:Status(true,true);
					end
				end
			);
		end
		-- THIS FUNCTIONS SAVES THE MODIFIED KEYS TO THE SENT, SO THEY ARE AVAILABLE WHEN COPIED WITH DUPLICATOR!
		t.Entity:UpdateKeys(_,_,size,immunity,strength,r,g,b,bubble,containment,passing_draw);
		return true;
	end
	if(not self:CheckLimit()) then return false end;
	local e = self:SpawnSENT(p,t,toggle,model,size,immunity,strength,r,g,b,bubble,containment,passing_draw);
	if(util.tobool(self:GetClientNumber("autolink"))) then
		self:AutoLink(e,t.Entity); -- Link to that energy system, if valid
	end
	--######## Weld things?
	local c = self:Weld(e,t.Entity,util.tobool(self:GetClientNumber("autoweld")));
	--######## Cleanup and undo register
	self:AddUndo(p,e,c);
	self:AddCleanup(p,c,e);
	return true;
end

--################# The PreEntitySpawn function is called before a SENT got spawned. Either by the duplicator or with the stool.@aVoN
function TOOL:PreEntitySpawn(p,e,toggle,model,size,immunity,strength_multiplier,r,g,b,bubble,containment,passing_draw,Strength)
	e:SetModel(model);
end

--################# The PostEntitySpawn function is called after a SENT got spawned. Either by the duplicator or with the stool.@aVoN
function TOOL:PostEntitySpawn(p,e,toggle,model,size,immunity,strength_multiplier,r,g,b,bubble,containment,passing_draw,Strength)
	e.ImmuneOwner = util.tobool(immunity);
	e.DrawBubble = util.tobool(bubble);
	e.PassingDraw = util.tobool(passing_draw);
	e.Containment = util.tobool(containment);
	e:SetSize(size or 80);
	e:SetMultiplier(strength_multiplier);
	if(toggle) then
		numpad.OnDown(p,toggle,"ToggleShield",e);
	end
	local num = tonumber(Strength);
	if(Strength and num and type(num) == "number") then
		e.Strength = num;
	end
	e:SetShieldColor(r,g,b);
end

--################# Controlpanel @aVoN
function TOOL:ControlsPanel(Panel)
	Panel:CheckBox("Autoweld","touchstone_autoweld");
	Panel:Help("This spawns the touchstone device. At the moment... this does not much except allows you to live underwater, in NON-LS gamemodes.");
end

--################# Register Stargate hooks. Needs to be called after all functions are loaded!
TOOL:Register();
